package com.wnhz.work.yz.wxapi;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.bean.WeiXinBean1;
import com.wnhz.work.yz.bean.WeiXinMessageBean;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.ShowPopupWindow;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ntop on 15/9/4.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "WXEntryActivity";
    private static final String SECRET = "dadad813cd07a6656995b891285192c4";
    private static final String WXAPP_ID = "wxb6172005514ce1e7";
    private String url_access_token = "https://api.weixin.qq.com/sns/oauth2/access_token?";
    private String url_refresh_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?";
    private String userinfo_url = "https://api.weixin.qq.com/sns/userinfo?";
    private Map<String, Object> map = new HashMap<>();
    private Map<String, Object> map_user = new HashMap<>();
    private Map<String, Object> map_refresh_token = new HashMap<>();
    private Gson gson;

    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
    private WeiXinMessageBean bean;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, WXAPP_ID, true);
        api.registerApp(WXAPP_ID);

        api.handleIntent(getIntent(), this);
        gson = new Gson();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {

    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        int result = 0;

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.e("resp", resp.getType() + "55");
                if (resp.getType() == 1) {

                    String code = ((SendAuth.Resp) resp).code;
                    System.out.println(code);
                    map.put("appid", WXAPP_ID);
                    map.put("secret", SECRET);
                    map.put("code", code);
                    map.put("grant_type", "authorization_code");
                    XUtil.Post(url_access_token, map, new MyCallBack<String>() {
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            Log.e("====微信回调11111", "onSuccess: " + result);

                            java.lang.reflect.Type type = new TypeToken<WeiXinBean1>() {
                            }.getType();

                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                WeiXinBean1 bean1 = new WeiXinBean1();
                                bean1.setAccess_token(jsonObject.optString("access_token"));
                                bean1.setExpires_in(jsonObject.optString("expires_in"));
                                bean1.setRefresh_token(jsonObject.optString("refresh_token"));
                                bean1.setOpenid(jsonObject.optString("openid"));
                                bean1.setScope(jsonObject.optString("scope"));
                                bean1.setUnionid(jsonObject.optString("unionid"));
                                MyApplication.getInstance().bean1 = bean1;

                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFinished() {
                            super.onFinished();

                        }
                    });

                } else if (resp.getType() == 255) {
                    System.out.println(255 + "");
                    finish();
                } else {
                    System.out.println("platform " + resp.getType());
                }


                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                break;
            default:
                break;
        }
        finish();
    }

    public Dialog dialog;

    public void showDialog() {

        if (null == dialog) {
            dialog = ShowPopupWindow.unCancleableDialog(this);
        }
        dialog.show();
    }

    public void closeDialog() {
        if (null != dialog) {
            dialog.dismiss();
        }
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
}
